package com.powernode.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.ProdProp;
import com.powernode.domain.ProdPropValue;
import com.powernode.model.Result;
import com.powernode.service.ProdPropService;
import com.powernode.service.ProdPropValueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author RenBoQing
 * @date 2024年07月27日 20:21
 * @Description 商品规格 管理  控制器
 */
@Api(tags = "商品规格接口管理")
@RequestMapping("product/spec")
@RestController
public class ProductSpecController {


    @Autowired
    private ProdPropService prodPropService;
    @Autowired
    private ProdPropValueService prodPropValueService;

    /*
     * 多条件分页查询商品规格
     * @author RenBoQing
     * @date 2024/7/27 20:47
     * @param current
     * @param size
     * @param propName
     * @param rule
     * @return com.powernode.model.Result<com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.powernode.domain.ProdProp>>
     */
    @ApiOperation("多条件分页查询商品规格")
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('prod:spec:page')")
    public Result<Page<ProdProp>> loadProductSpecPage(@RequestParam(defaultValue = "1") Long current,
                                                      @RequestParam(defaultValue = "10") Long size,
                                                      @RequestParam(required = false) String propName) {
        Page<ProdProp> page = prodPropService.loadProductSpecPage(current, size, propName);
        return Result.success(page);
    }

    /*
     * 新增商品规格
     * @author RenBoQing
     * @date 2024/7/27 20:47
     * @param productSpec
     * @return com.powernode.model.Result<java.lang.String>
     */
    @ApiOperation("新增商品规格")
    @PostMapping("")
    @PreAuthorize("hasAuthority('prod:spec:save')")
    public Result<String> saveProductSpec(@RequestBody ProdProp productSpec) {
        // 新增商品规格的同时 也需要新增商品规格值 也就是属性值
        boolean success = prodPropService.saveProductSpec(productSpec);
        return Result.handle(success);
    }
    /*
     * 修改商品规格
     * @author RenBoQing
     * @date 2024/7/27 20:47
     * @param productSpec
     * @return com.powernode.model.Result<java.lang.String>
     */
    @ApiOperation("修改商品规格")
    @PutMapping("")
    @PreAuthorize("hasAuthority('prod:spec:update')")
    public Result<String> updateProductSpec(@RequestBody ProdProp productSpec) {
        boolean success = prodPropService.updateProductSpec(productSpec);
        return Result.handle(success);
    }
    /*
     * 删除商品规格
     * @author RenBoQing
     * @date 2024/7/27 20:47
     * @param id
     * @return com.powernode.model.Result<java.lang.String>
     */
    @ApiOperation("删除商品规格")
    @DeleteMapping("/{propId}")
    @PreAuthorize("hasAuthority('prod:spec:delete')")
    public Result<String> removeProductSpec(@PathVariable Long propId) {
        boolean success = prodPropService.removeProductSpecById(propId);
        return Result.handle(success);
    }

    /*
     * 查询商品规格详情属性集合
     * @author RenBoQing
     * @date 2024/7/27 20:47
     * @param id
     * @return com.powernode.model.Result<com.powernode.domain.ProdProp>
     */
    @ApiOperation("查询商品规格详情集合")
    @GetMapping("/info/{propId}")

    @PreAuthorize("hasAuthority('prod:spec:page')")
    public Result<List<ProdProp>> queryProductSpecList() {
        List<ProdProp> queryProductSpecList = prodPropService.queryProductSpecList();
        return Result.success(queryProductSpecList);
    }

    /*
     * 根据商品属性值的id查询属性值
     * @author RenBoQing
     * @date 2024/7/28 13:44
     * @param propId
     * @return com.powernode.model.Result<java.util.List<com.powernode.domain.ProdPropValue>>
     */
    @ApiOperation("根据商品属性值的id查询属性值集合")
    @GetMapping("/listSpecValue/{propId}")
    @PreAuthorize("hasAuthority('prod:spec:page')")
    public Result<List<ProdPropValue>> getProductSpecById(@PathVariable Long propId) {
        List<ProdPropValue> prodPropList = prodPropValueService.list(new LambdaQueryWrapper<ProdPropValue>().eq(ProdPropValue::getPropId, propId)
        );
        return Result.success(prodPropList);
    }
}
